import { IMovie } from "../db/MovieSchema";
import { Movie } from "../entities/Movie";
import { MovieModel } from "../db";
import { SearchCondition } from "../entities/SearchCondition";
import { ISearchResult } from "../entities/CommonTypes";

export class MoiveService {
  public static async add(movie: Movie): Promise<IMovie | string[]> {
    movie = Movie.transform(movie);
    const errors = await movie.validateThis();
    if (errors.length > 0) {
      return errors;
    }

    return await MovieModel.create(movie);
  }

  public static async edit(id: string, movie: Movie): Promise<string[]> {
    const movieObj = Movie.transform(movie);
    const errors = await movieObj.validateThis(true);
    if (errors.length > 0) {
      return errors;
    }

    await MovieModel.updateOne({ _id: id }, movie);
    return errors;
  }

  public static async delete(id: string): Promise<void> {
    await MovieModel.deleteOne({ _id: id });
  }

  public static async findById(id: string): Promise<IMovie | null> {
    return await MovieModel.findById(id);
  }

  public static async find(condition: SearchCondition): Promise<ISearchResult<Movie>> {
    const conditionObj = SearchCondition.transform(condition);
    const errors = await conditionObj.validateThis(true);
    if (errors.length > 0) {
      return {
        count: 0,
        data: [],
        errors,
      };
    }

    const movies = await MovieModel.find({
      name: { $regex: new RegExp(conditionObj.key) },
    })
      .skip((conditionObj.page - 1) * conditionObj.limit)
      .limit(conditionObj.limit);

    const count = await MovieModel.find({
      name: { $regex: new RegExp(conditionObj.key) },
    }).countDocuments();

    return {
      count,
      data: movies,
      errors: [],
    };
  }
}
